package com.example.piece.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.example.piece.R
import com.example.piece.databinding.ItemTikTokBinding
import com.example.piece.entity.AttentionEntity
import com.example.piece.tools.cache.PreloadManager
import com.wenming.library.encryption.imp.AESEncode
import com.example.piece.BR

class TiktokAdapter : PagerAdapter {
    var data: List<AttentionEntity>

    constructor(data: List<AttentionEntity>, clickListener: View.OnClickListener) : super() {
        this.data = data
        this.mViewPool = mutableListOf<View>()
        this.clickListener=clickListener
    }

    /**
     * View缓存池，从ViewPager中移除的item将会存到这里面，用来复用
     */
    private val mViewPool: MutableList<View>
    override fun getCount(): Int = data.size
    var clickListener: View.OnClickListener? = null

    override fun isViewFromObject(view: View, `object`: Any): Boolean {

        val binding: ViewDataBinding = `object` as ViewDataBinding
        return view == binding.root
    }



    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val context = container.context

        val binding = DataBindingUtil.inflate<ItemTikTokBinding>(
            LayoutInflater.from(context),
            R.layout.item_tik_tok,
            container,
            false
        )

        binding.setVariable(BR.position, position)

        var view: View? = null
        if (mViewPool.size > 0) {
            view = mViewPool.get(0)
            mViewPool.removeAt(0)
        }

        view = binding?.root
        val attentionEntity = data[position]
        //开始预加载
        val videoUrl =
            AESEncode.Decrypt(
                attentionEntity?.joke?.videoUrl?.split(",")?.get(0)?.replace("ftp://", "")
                    .toString()
                    .trim(),
                "cretinzp**273846"
            )


        PreloadManager.getInstance(context).addPreloadTask(videoUrl, position)
        val thumbUrl =
            AESEncode.Decrypt(
                attentionEntity?.joke?.thumbUrl?.split(",")?.get(0)?.replace("ftp://", "")
                    .toString()
                    .trim(),
                "cretinzp**273846"
            )
        val thumb = binding?.tiktokView?.findViewById(R.id.iv_thumb) as ImageView
        Glide.with(context)
            .load(thumbUrl)
            .placeholder(R.color.black)
            .into(thumb)
        val textView = binding?.tiktokView?.findViewById(R.id.tv_title) as TextView
        textView.text = attentionEntity?.joke?.content
        val tv_like = binding?.tiktokView?.findViewById(R.id.tv_like) as TextView
        tv_like.tag=position

        tv_like.text = attentionEntity?.info?.likeNum.toString()
        tv_like.setOnClickListener(clickListener)

        tv_like.isSelected = attentionEntity?.info?.isLike == true

        val tv_pinglun = binding?.tiktokView?.findViewById(R.id.tv_pinglun) as TextView
        tv_pinglun.tag=position
        tv_pinglun.text = attentionEntity?.info?.commentNum.toString()
        tv_pinglun.setOnClickListener(clickListener)

        val tv_share = binding?.tiktokView?.findViewById(R.id.tv_share) as TextView
        tv_share.tag=position
        tv_share.text = attentionEntity?.info?.shareNum.toString()
        tv_share.setOnClickListener(clickListener)

        container.addView(view)
        return binding

    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {

        var binding: ViewDataBinding = `object` as ViewDataBinding
        container.removeView(binding.root)
        val attentionEntity = data[position]
        //取消预加载
        val videoUrl =
            AESEncode.Decrypt(
                attentionEntity?.joke?.videoUrl?.split(",")?.get(0)?.replace("ftp://", "")
                    .toString()
                    .trim(),
                "cretinzp**273846"
            )
        PreloadManager.getInstance(container.context).removePreloadTask(videoUrl)
        mViewPool.add(binding.root)

    }


}